//
//  ImageListController.m
//  HWSGW
//
//  Created by avachen on 15/11/5.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "ImageListController.h"
#import "ImageModel.h"
#import "UIImageView+WebCache.h"
#import "PhotoViewController.h"
#import "HWNavigationController.h"

@interface ImageListController ()

@end

@implementation ImageListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图片新闻";
    [self requestData];
    
    // Do any additional setup after loading the view.
}
- (void)requestData{
    NSArray *array = [HWDataService requestData:image_list];
    _data = [[NSMutableArray alloc] initWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        ImageModel *imageModel = [[ImageModel alloc] initContentWithDic:dic];
        
        [_data addObject:imageModel];
        
        
    }
}
#pragma mark - UICollectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:100];
    ImageModel*imgModel = self.data[indexPath.item];
    [image sd_setImageWithURL:[NSURL URLWithString:imgModel.image]];
    
    return cell;
}
//点击单元格实现的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *urls = [NSMutableArray array];
//    for (ImageModel *imageModel in self.data) {
//        NSString *urlsString =  imageModel.image;
//        [urls addObject:urlsString];
//        
//    }
    //读取图片的url数据存入数组urls中
    for (int i = 0; i < _data.count; i++) {
        ImageModel *imageModel = self.data[i];
        NSString *urlString = imageModel.image;
        [urls addObject:urlString];
    }
    //创建模态视图
    PhotoViewController *photoCtrl = [[PhotoViewController alloc] init];
    //创建导航控制器
    HWNavigationController *navigation = [[HWNavigationController alloc] initWithRootViewController:photoCtrl];
    
    //通过模态视图效果弹出控制器，此控制器是直接被添加到window上的，是没有父类控制器的
    [self presentViewController:navigation animated:YES completion:NULL];
    //传递图片数组数据
    photoCtrl.urls = urls;
    photoCtrl.indexPath = indexPath;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
